package com.sysu.simulation;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sysu.simulation.bean.User;
import com.sysu.simulation.helper.AccountHelper;
import com.sysu.simulation.interfaces.OnTabReselectListener;

public class InfoFragment extends BaseFragment implements View.OnClickListener, OnTabReselectListener{

    private LinearLayout mItemClothes;
    private ImageView mPortrait;
    private TextView mNickname;
    private TextView mHeightValue;
    private TextView mWeightValue;
    private TextView mGenderValue;
    private LinearLayout mbodyInfoContainer;
    private LinearLayout mLogoutButton;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_info;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);

        /* bind views */
        mItemClothes = (LinearLayout) findView(R.id.item_clothes);
        mPortrait = (ImageView) findView(R.id.portrait);
        mNickname = (TextView) findView(R.id.nickname);
        mHeightValue = (TextView) findView(R.id.value_height);
        mWeightValue = (TextView) findView(R.id.value_weight);
        mGenderValue = (TextView) findView(R.id.value_gender);
        mbodyInfoContainer = (LinearLayout) findView(R.id.body_info_container);
        mLogoutButton = (LinearLayout) findView(R.id.item_logout);
        /* end bind views */

        /* bind listeners */
        mItemClothes.setOnClickListener(this);
        mLogoutButton.setOnClickListener(this);
        mPortrait.setOnClickListener(this);
        mbodyInfoContainer.setOnClickListener(this);
        /* end bind listeners */
    }

    @Override
    protected void initData() {
        super.initData();
        requestUserCache();
    }

    @Override
    public void onResume() {
        super.onResume();
        /*if (AccountHelper.isLogin()) {
            User user = AccountHelper.getUser();
            updateView(user);
        } else {
            hideView();
        }*/
        requestUserCache();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!AccountHelper.isLogin()) {
            hideView();
        }
    }

    /**
     * if user isLogin,request user cache,
     * And then request user info and update user info
     */
    private void requestUserCache() {
        if (AccountHelper.isLogin()) {
            //Toast.makeText(mContext, "已登录", Toast.LENGTH_SHORT).show();
            User user = AccountHelper.getUser();
            updateView(user);
            sendRequestData();
        } else {
            hideView();
        }
    }

    private void sendRequestData() {

    }

    private void updateView(User userInfo) {
        mNickname.setText(userInfo.getNickname());
        mHeightValue.setText(String.valueOf(userInfo.getHeight()));
        mWeightValue.setText(String.valueOf(userInfo.getWeight()));
        switch (userInfo.getGender()) {
            case 0:
                mGenderValue.setText("男");
                break;
            case 1:
                mGenderValue.setText("女");
                break;
            default:
                mGenderValue.setText("--");
                break;
        }
        mbodyInfoContainer.setVisibility(View.VISIBLE);
        mLogoutButton.setVisibility(View.VISIBLE);
    }

    private void hideView() {
        mNickname.setText("点击登录");
        mbodyInfoContainer.setVisibility(View.GONE);
        mLogoutButton.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        if(!AccountHelper.isLogin()) {
            LoginActivity.show(getActivity());
            return;
        }

        int id = v.getId();
        switch (id) {
            case R.id.portrait:
                EditUserActivity.show(mContext);
                break;
            case R.id.body_info_container:
                EditUserActivity.show(mContext);
                break;
            case R.id.item_logout:
                AccountHelper.logout(mLogoutButton);
                hideView();
                break;
            case R.id.item_clothes:
                ClothesActivity.show(mContext);
                break;
            default:
                break;
        }
    }

    @Override
    public void onTabReselect() {

    }
}
